<?php
/*
 * @分销商城
 * @功能说明：登陆跳转
 * @更新说明：暂无更新
 * @文件名 IndexController.class.php
 * @编码 UTF-8
 * @创建时间 2016-4-12 下午4:05:07
 * @创建人 XING
 */

namespace pc\Controller;
use Think\Controller;
class IndexController extends Controller {

    /*
     * 销券首页
     * @date: 2017-12-2
     */
    public function index(){
        if(!is_login()){
            $this->redirect('/user/login');
        }
        $userMsg = M('NurseEntity')
            ->where('id = '.session('NurseUid'))->find();
        if($userMsg['user_status']){
            session('NurseName',null);
            session('NurseUid',null);
            session('NurseMobile',null);
            $this->success('您的帐号未审核通过或账户状态不对，请联系客服！','/user/login');
        }
        //从系统配置获取可操作的项目
        $projects = explode(',',getSysConfig('project'));
        //查询销券历史记录
        $history = M('HistorySelect')->where(['nurse_id'=>session('NurseUid'),'quan_type'=>1])->order('update_at desc')->limit(30)->select();
        $this->assign('history',$history);
        $this->assign('projects',$projects);
        $this->assign('operator',$userMsg['uname']);
        $this->display('index/qxww_stamps_search');
    }

    /*
     * 修补页
     * @date: 2017-12-2
     */
    public function repair(){
        if(!is_login()){
            $this->redirect('/user/login');
        }
        $userMsg = M('NurseEntity')
            ->where('id = '.session('NurseUid'))->find();
        //查询销券历史记录
        $history = M('HistorySelect')->where(['nurse_id'=>session('NurseUid'),'quan_type'=>2])->order('update_at desc')->limit(30)->select();
        //从系统配置获取可操作的项目
        $projects = explode(',',getSysConfig('project'));
        $this->assign('history',$history);
        $this->assign('projects',$projects);
        $this->assign('operator',$userMsg['uname']);
        $this->display('index/qxww_repair_search');
    }
    /*
     * 销券查询修补
     * @date: 2017-12-2
     */
    public function search_ticket(){
        $security_code = I('post.security_code');
        $card_number = 'QX'.I('post.card_number');
        if($security_code && $card_number){
            $security_code = md5(I('post.security_code').'QXww!@#');
            $card = M('CardInfo')->where(['card_number'=>$card_number,'security_code'=>$security_code])->find();
            $history = M('HistorySelect');
            $wdata = ['card_number'=>$card_number,'quan_type'=>1,'nurse_id'=>session('NurseUid'),'type'=>1];
            //新增一条销券记录
            $old = $history->where($wdata)->find();
            if($old){
                $history->where($wdata)->setInc('times');
                $history->where($wdata)->setField(['update_at'=>time()]);
            }else{
                $wdata['times'] = 1;
                $wdata['add_at'] = time();
                $wdata['update_at'] = time();
                $history->add($wdata);
            }
            //返回状态的几种情况 1 有此券未销券 2 已销券 3 无此券
            if($card){
                if($card['card_status']){
                    dataFormat(2, '0', 'success');
                }else{
                    dataFormat(1, '0', 'success');
                }
            }else{
                dataFormat(3, '0', 'success');
            }
        }else{
            dataFormat(0, '-1', 'error'); //参数缺失
        }
    }

    /*
     * 销券查询修补
     * @date: 2017-12-2
     */
    public function search_xiubu(){
        $card_number = 'QX'.I('post.card_number');
        $user_content = I('post.user_content');
        $card = M('CardInfo')->where(['card_number'=>$card_number,'user_name'=>$user_content])->find();
        if(!$card){
            $card = M('CardInfo')->where(['card_number'=>$card_number,'user_mobile'=>$user_content])->find();
        }
        if($card){
            $history = M('HistorySelect');
            $wdata = ['card_number'=>$card_number,'quan_type'=>2,'nurse_id'=>session('NurseUid'),'type'=>1];
            //新增一条查询记录
            $old = $history->where($wdata)->find();
            if($old){
                $history->where($wdata)->setInc('times');
                $history->where($wdata)->setField(['update_at'=>time()]);
            }else{
                $wdata['times'] = 1;
                $wdata['type'] = 1;
                $wdata['quan_type'] = 2;
                $wdata['add_at'] = time();
                $wdata['update_at'] = time();
                $history->add($wdata);
            }
            //返回状态的几种情况 3 姓名和手机号不匹配 1 可修补 2 已修补
            if($card['card_status'] && $card['xiubu_at']){
                $xiubuData = ['card_number'=>$card['card_number'],'user_name'=>$card['xiubu_uname'],'project'=>$card['xiubu_project'],'use_at'=>date('Y-m-d',$card['xiubu_at']),'is_xiubu'=>0];
            }else{
                $xiubuData = ['card_number'=>$card['card_number'],'user_name'=>$card['user_name'],'project'=>$card['use_project'],'use_at'=>date('Y-m-d',$card['use_at']),'is_xiubu'=>1];
            }
            dataFormat($xiubuData, '0', 'success');
        }else{
            dataFormat(3, '1', 'error'); //姓名和手机号不匹配
        }
    }

    /*
	 * 处理销券
	 * @date: 2017-3-18 上午11:05:26
	 * @editor: zyy
	 */
    public function useCard(){
        //已销券成功的返回错误信息
        $card_number = 'QX'.I('post.card_number');
        $security_code = md5(I('post.security_code').'QXww!@#');
        $use_status = M('CardInfo')->where(['card_number'=>$card_number,'security_code'=>$security_code,'card_status'=>1])->find();
        if($use_status){
            dataFormat(['msg'=>'不能重复销券','url'=>'/index/index'], '1', 'error');
        }
        $history = M('HistorySelect');
        $wdata = ['card_number'=>$card_number,'nurse_id'=>session('NurseUid'),'quan_type'=>1,'type'=>2];
        //非空验证
        if(!I('post.user_name') && !I('post.user_mobile') && !I('post.use_project') && !I('post.use_at')){
            dataFormat(['msg'=>'参数不能为空','url'=>'/index/index'], '1', 'error'); //姓名和手机号不匹配
        }
        //新增一条销券记录
        $old = $history->where($wdata)->find();
        if($old){
            $history->where($wdata)->setInc('times');
            $history->where($wdata)->setField(['update_at'=>time()]);
        }else{
            $wdata['times'] = 1;
            $wdata['type'] = 2;
            $wdata['add_at'] = time();
            $wdata['update_at'] = time();
            $history->add($wdata);
        }

        //更新卡券消费信息
        $rs = M('CardInfo')->where(['card_number'=>$card_number,'security_code'=>$security_code])->setField(array(
            'card_status'   => 1, //第一次销券 已使用未修补
            'type'          => 2, //1查询请求 2销券请求
            'nurse_id'   => session('NurseUid'), // 操作人
            'user_name'     => trim(I('post.user_name')),
            'user_mobile'     => trim(I('post.user_mobile')),
            'use_project' => trim(I('post.use_project')),
            'use_at' => strtotime(I('post.use_at')),
            'valid_at' => (strtotime(I('post.use_at'))+24*3600*365), //修补有效期
            'update_at'     => time(),
        ));

            if($rs){
            //将销券记录加入录入信息表
                    M('InputInfo')->add(array(
                        'card_number'   => $card_number,
                        'user_name'     => trim(I('post.user_name')),
                        'nurse_id'      => session('NurseUid'),
                        'input_name'    => session('NurseName'),
                        'user_mobile'   => trim(I('post.user_mobile')),
                        'use_project'   => trim(I('post.use_project')),
                        'quan_type'     => 1,
                        'add_at'        => time(),
                        'update_at'     => time(),
                    ));
                dataFormat(['msg'=>'销券成功','url'=>'/index/index'], '0', 'success');
            }else{
                dataFormat(['msg'=>'销券失败','url'=>'/index/index'], '1', 'error');
            }

    }

    /*
	 * 本人修补
	 * @date: 2017-12-04 下午1:05:26
	 * @editor: zyy
	 */
    public function self_xiubu(){
        if(I('post.card_number') && I('post.user_name')){
            $xiubuData = M('CardInfo')->where(['card_number'=>I('post.card_number'),'user_name'=>I('post.user_name')])->find();
            //判断是否在有效期内
            if($xiubuData['valid_at'] < time()){
                dataFormat(['msg'=>'已过期','url'=>'/index/repair'], '1', 'error');
            }
            //已销券成功的返回错误信息
            if($xiubuData['card_status'] == 3){
                dataFormat(['msg'=>'只能修补一次','url'=>'/index/repair'], '1', 'error');
            }
            //销券历史记录
            $history = M('HistorySelect');
            $wdata = ['card_number'=>$xiubuData['card_number'],'nurse_id'=>session('NurseUid'),'quan_type'=>2,'type'=>2];
            $old = $history->where($wdata)->find();
            if($old){
                $history->where($wdata)->setInc('times');
                $history->where($wdata)->setField(['update_at'=>time()]);
            }else{
                $wdata['times'] = 1;
                $wdata['type'] = 2;
                $wdata['add_at'] = time();
                $wdata['update_at'] = time();
                $history->add($wdata);
            }
            $rs = M('CardInfo')->where(['card_number'=>I('post.card_number'),'user_name'=>I('post.user_name')])->setField([
                'card_status'   => 3, //修补
                'type'          => 2, //1查询请求 2销券请求
                'nurse_id'      => session('NurseUid'), // 操作人
                'xiubu_uname'   => $xiubuData['user_name'],
                'xiubu_mobile'  => $xiubuData['user_mobile'],
                'xiubu_project' => $xiubuData['use_project'],
                'xiubu_at'      => time(),
                'update_at'     => time(),
            ]);
            if($rs){
                //将销券记录加入录入信息表
                M('InputInfo')->add(array(
                    'card_number'   => I('post.card_number'),
                    'user_name'     => $xiubuData['user_name'],
                    'nurse_id'      => session('NurseUid'),
                    'input_name'    => session('NurseName'),
                    'user_mobile'   => $xiubuData['user_mobile'],
                    'use_project'   => $xiubuData['use_project'],
                    'quan_type'     => 2,
                    'xiubu_type'    => 1,
                    'add_at'        => time(),
                    'update_at'     => time(),
                ));
                dataFormat(['msg'=>'修补成功','url'=>'/index/repair'], '0', 'success');
            }
        }else{
            dataFormat(['msg'=>'参数缺失','url'=>'/index/repair'], '1', 'error');
        }
    }

    /*
	 * 他人修补
	 * @date: 2017-12-04 下午1:05:26
	 * @editor: zyy
	 */
    public function another_xiubu(){
        if(I('post.xiubu_uname') && I('post.xiubu_mobile') && I('post.xiubu_project')){
            $xiubuData = M('CardInfo')->where(['card_number'=>I('post.card_number'),'user_name'=>I('post.user_name')])->find();
            //判断是否在有效期内
            if($xiubuData['valid_at'] < time()){
                dataFormat(['msg'=>'已过期','url'=>'/index/repair'], '1', 'error');
            }
            //已销券成功的返回错误信息
            if($xiubuData['card_status'] == 3){
                dataFormat(['msg'=>'只能修补一次','url'=>'/index/repair'], '1', 'error');
            }
            //销券历史记录
            $history = M('HistorySelect');
            $wdata = ['card_number'=>$xiubuData['card_number'],'security_code'=>$xiubuData['security_code'],'nurse_id'=>session('NurseUid'),'quan_type'=>2,'type'=>2];
            $old = $history->where($wdata)->find();
            if($old){
                $history->where($wdata)->setInc('times');
                $history->where($wdata)->setField(['update_at'=>time()]);
            }else{
                $wdata['times'] = 1;
                $wdata['type'] = 2;
                $wdata['add_at'] = time();
                $wdata['update_at'] = time();
                $history->add($wdata);
            }
            $rs = M('CardInfo')->where(['card_number'=>I('post.card_number'),'user_name'=>I('post.user_name')])->setField([
                'card_status'   => 3, //修补
                'type'          => 2, //1查询请求 2销券请求
                'nurse_id'      => session('NurseUid'), // 操作人
                'xiubu_uname'   => I('post.xiubu_uname'),
                'xiubu_mobile'  => I('post.xiubu_mobile'),
                'xiubu_project' => I('post.xiubu_project'),
                'xiubu_type'    => 1,
                'xiubu_at'      => time(),
                'update_at'     => time(),
            ]);
            if($rs){
                //将销券记录加入录入信息表
                M('InputInfo')->add(array(
                    'card_number'   => I('post.card_number'),
                    'user_name'     => trim(I('post.xiubu_uname')),
                    'nurse_id'      => session('NurseUid'),
                    'input_name'    => session('NurseName'),
                    'user_mobile'   => trim(I('post.xiubu_mobile')),
                    'use_project'   => trim(I('post.xiubu_project')),
                    'quan_type'     => 2,
                    'xiubu_type'    => 2,
                    'add_at'        => time(),
                    'update_at'     => time(),
                ));
                dataFormat(['msg'=>'修补成功','url'=>'/index/repair'], '0', 'success');
            }
        }else{
            dataFormat(['msg'=>'参数缺失','url'=>'/index/repair'], '1', 'error');
        }
    }

    /**
     * 请求表里超时的新增到录入表
    */
    public function timeout_input_info(){
        $time_out_req = M('CwwRequest')->where(['type'=>2,'request_status'=>1,'is_timeout'=>1])->order('id desc')->limit(2)->select();
//        var_dump($time_out_req);exit;
        if($time_out_req){
            foreach ($time_out_req as $value)
            {
                $nurse = M('NurseEntity')->where(['mobile'=>$value['operater_mobile']])->find();
                //判断录入表是否有该卡券
                $input = M('InputInfo')->where(['card_number'=>$value['card_number']])->find();
                if(!$input){
                    $rs = M('InputInfo')->add(array(
                        'card_number'   => $value['card_number'],
                        'user_name'     => $value['user_name'],
                        'nurse_id'      => $nurse['id'],
                        'input_name'    => $value['operater_mobile'],
                        'quan_type'     => $value['quan_type'],
                        'input_time'    => time(),
                        'group_address' => $value['group_address'],
                        'add_at'        => time(),
                        'update_at'     => time(),
                    ));
                    if($rs){
                        M('CwwRequest')->where(['id'=>$value['id']])->setField(['is_timeout'=>2]);
                    }
                }

            }
        }
        echo '执行成功';
    }
    
}